﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Crawler.Model;
using Husb.Framework.DataAccess;
using System.Data;

using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Crawler.DataAccess
{
    public partial class WebPageDao
    {
        public WebPageDao()
        {
            //ObjectId = "Id";
            //MasterObjectId = "";
            //DetailObjectId = "";
            CommandType = System.Data.CommandType.StoredProcedure;

            GetByIdStatement = "Crawler.WebPages_SelectWebPage";
            //GetByNameStoredProc = SR.SelectWebPageByName;
            FindAllStatement = "Crawler.WebPages_SelectWebPagesAll";
            //FindStoredProc = SR.SelectWebPagesDynamic;
            //FindByNameStoredProc = SR.SelectWebPagesByNameInexact;


            InsertStatement = "Crawler.WebPages_InsertWebPage";
            UpdateStatement = "Crawler.WebPages_UpdateWebPage";
            //CreateUpdateStoredProc = SR.InsertUpdateWebPage;
            DeleteByIdStatement = "Crawler.WebPages_DeleteWebPage";
            //DeleteByObjectStoredProc = SR.DeleteWebPage;

            //GetRowCountStoredProc = SR.SelectWebPagesRowCount;
            //GetPagedSortedProc = SR.SelectWebPagesPaged;
        }

        protected override void LoadProperty(WebPageInfo webPage, System.Data.IDataReader dr)
        {
            webPage.Id = (System.Guid)dr["Id"];
            webPage.Name = (System.String)dr["Name"];
            webPage.CategoryId = (System.Guid)dr["CategoryId"];
            webPage.Category = dr["Category"] == DBNull.Value ? "" : (System.String)dr["Category"];
            webPage.Url = dr["Url"] == DBNull.Value ? "" : (System.String)dr["Url"];
            webPage.ContentText = dr["ContentText"] == DBNull.Value ? "" : (System.String)dr["ContentText"];
            //webPage.ContentHTML = dr["ContentHTML"] == DBNull.Value ? new byte[] { } : (System.Byte[])dr["ContentHTML"];
            webPage.CreatedDate = dr["CreatedDate"] == DBNull.Value ? DateTime.MinValue : (System.DateTime?)dr["CreatedDate"];
            webPage.Tags = dr["Tags"] == DBNull.Value ? "" : (System.String)dr["Tags"];
        }

        protected override void PopulateParamters(WebPageInfo webPage, Database db, System.Data.Common.DbCommand cmd)
        {
            db.AddInParameter(cmd, "Id", DbType.Guid, webPage.Id);
            db.AddInParameter(cmd, "Name", DbType.String, webPage.Name);
            db.AddInParameter(cmd, "CategoryId", DbType.Guid, webPage.CategoryId);
            db.AddInParameter(cmd, "Category", DbType.AnsiString, webPage.Category);
            db.AddInParameter(cmd, "Url", DbType.AnsiString, webPage.Url);
            db.AddInParameter(cmd, "ContentText", DbType.String, webPage.ContentText);
            db.AddInParameter(cmd, "ContentHTML", DbType.Binary, webPage.ContentHTML);
            db.AddInParameter(cmd, "CreatedDate", DbType.DateTime, webPage.CreatedDate);
            db.AddInParameter(cmd, "Tags", DbType.AnsiString, webPage.Tags);
        }

        #region LoadHalfWebPageInfo
        /// <summary>
        /// 根据执行不同的数据获取命令，得到一个不完整的WebPageInfo<see cref="WebPageInfo" />对象，也许只有部分属性有内容。
        /// </summary>
        /// <param name="webPage"></param>
        /// <param name="dr">IDataReader<see cref="IDataReader" /></param>
        public static void LoadPartialProperty(WebPageInfo webPage, System.Data.IDataReader dr)
        {
            for (int fieldCount = 0; fieldCount < dr.FieldCount; fieldCount++)
            {
                switch (dr.GetName(fieldCount))
                {
                    case "Id":
                        webPage.Id = (System.Guid)dr["Id"];
                        break;
                    case "Name":
                        webPage.Name = (System.String)dr["Name"];
                        break;
                    case "CategoryId":
                        webPage.CategoryId = (System.Guid)dr["CategoryId"];
                        break;
                    case "Category":
                        webPage.Category = dr["Category"] == DBNull.Value ? "" : (System.String)dr["Category"];
                        break;
                    case "Url":
                        webPage.Url = dr["Url"] == DBNull.Value ? "" : (System.String)dr["Url"];
                        break;
                    case "ContentText":
                        webPage.ContentText = dr["ContentText"] == DBNull.Value ? "" : (System.String)dr["ContentText"];
                        break;
                    case "ContentHTML":
                        webPage.ContentHTML = dr["ContentHTML"] == DBNull.Value ? new byte[] { } : (System.Byte[])dr["ContentHTML"];
                        break;
                    case "CreatedDate":
                        webPage.CreatedDate = dr["CreatedDate"] == DBNull.Value ? DateTime.MinValue : (System.DateTime?)dr["CreatedDate"];
                        break;
                    case "Tags":
                        webPage.Tags = dr["Tags"] == DBNull.Value ? "" : (System.String)dr["Tags"];
                        break;
                }
            }
        }
        #endregion
    }
}
